# -*- coding: utf-8 -*-
import subprocess

import redis
from scrapy_redis.spiders import RedisSpider
from scrapy import Request, FormRequest
from spider.ops import Ops
import re
from facility.reg_fn import get_number

from facility.time_fn import timestamp2string
from facility.reg_fn import get_number


def longest1(s):
    t = re.findall('\d+', s)
    if t:
        return max(t, key=len)
    return ''


class SichouzhiluSpider(RedisSpider):
    name = 'sichouzhilu'
    redis_key = 'sichouzhilu:start_urls'
    v8host = 'http://localhost:5000/'

    con = redis.Redis(host='localhost', port=6379, db=0)
    # con = redis.Redis(host='localhost', port=6379, db=0, password='pass')
    con.lpush(redis_key, v8host)

    def parse(self, response):
        yield FormRequest(url='http://www.zgsljf.com/lend/Default.aspx',
                          formdata={
                              '__VIEWSTATE': "/wEPDwUJODAwOTk2NzU1D2QWAgIBD2QWAgIBDw8WAh4LUmVjb3JkY291bnQCyQFkZGRmmhFrbY2j2rG+c/AiEk+of8sOvg==",
                              '__VIEWSTATEGENERATOR': '1E288709',
                              '__EVENTTARGET': "AspNetPager1",
                              '__EVENTARGUMENT': '1',
                              'AspNetPager1_input': '1'
                              },
                          dont_filter=True, callback=self.parse_item)

    def parse_item(self, response):
        q = Ops(response)
        a = q.get_attr('//*[@id="AspNetPager1"]/a[14]', 'href')
        total_page = int(a.split(",")[1].split(")")[0].split("'")[1])
        print('total_page', total_page)

        for i in range(total_page):
            yield FormRequest(url='http://www.zgsljf.com/lend/Default.aspx',
                              formdata={
                                  '__VIEWSTATE': "/wEPDwUJODAwOTk2NzU1D2QWAgIBD2QWAgIBDw8WAh4LUmVjb3JkY291bnQCxwFkZGTSkjkgjFald33bY+BtUouhqwrhaQ==",
                                  '__VIEWSTATEGENERATOR': '1E288709',
                                  '__EVENTTARGET': "AspNetPager1",
                                  '__EVENTARGUMENT': '{}'.format(i + 1),
                                  'AspNetPager1_input': '1'
                              },
                              dont_filter=True, callback=self.parse_end)

    # pass
    def parse_end(self, response):
        q = Ops(response)

        num = q.get_num('//*[@id="b_i"]/ul')
        if num > 1:
            for i in range(num - 1):
                start = str(get_number(q.get_str('//*[@id="b_i"]/ul[{}]/li[1]/a'.format(i + 2)))[-1])
                item = {'name': q.get_str('//*[@id="b_i"]/ul[{}]/li[1]/a'.format(i + 2)),
                        'amount': q.get_str('//*[@id="b_i"]/ul[{}]/li[2]/span[1]/b'.format(i + 2)),
                        'rate': q.get_str('//*[@id="b_i"]/ul[{}]/li[2]/span[2]/b'.format(i + 2)),
                        "start": start[0:4] + "-" + start[4:6] + "-" + start[6:8],
                        'cycle': q.get_str('//*[@id="b_i"]/ul[{}]/li[2]/span[3]/b'.format(i + 2)) + ''.join(
                            q.xpath('//*[@id="b_i"]/ul[{}]/li[2]/span[3]/text()'.format(i + 2))),
                        'way': q.get_str('//*[@id="b_i"]/ul[{}]/li[2]/span[4]'.format(i + 2)),
                        'no': q.get_str('//*[@id="b_i"]/ul[{}]/li[1]/a'.format(i + 2))
                        }

                subprocess.call("echo  {} >>丝绸之路.txt".format(' '.join(item.values()).replace('个月', '').strip()),
                                shell=True)
